package com.metroinfo.kernel;

import com.metroinfo.infrastructure.MainDataStructure;
import com.metroinfo.infrastructure.keys.BusInfoDataKey;
import com.metroinfo.infrastructure.keys.BusLineDataKey;
import com.metroinfo.infrastructure.keys.StationInfoDataKey;
import com.metroinfo.infrastructure.value.BusInfoValue;
import com.metroinfo.infrastructure.value.BusLastAppearanceValue;
import com.metroinfo.infrastructure.value.BusLineValue;
import com.metroinfo.infrastructure.value.StationInfoValue;
import com.metroinfo.infrastructure.value.StationLineValue;
import com.metroinfo.kernel.data.ResponseData;
import com.metroinfo.kernel.interfaces.DistanceCalc;
import com.metroinfo.kernel.interfaces.MessagePassing;
import com.metroinfo.parser.GeneralSettings;
import com.metroinfo.parser.data.GeneralRequest;
import com.metroinfo.parser.data.SendDataPar;

public class ProcessData {

	GeneralRequest generalRequest;
	private MainDataStructure mainDataStructure;
	private DistanceCalc distanceCalculator;
	private MessagePassing massagePassing;
	
	public ProcessData(MainDataStructure mds){
		this.mainDataStructure = mds;
	}
	
	public MessagePassing getMassagePassing() {
		return massagePassing;
	}

	public void setMassagePassing(MessagePassing massagePassing) {
		this.massagePassing = massagePassing;
	}

	public DistanceCalc getDistanceCalculator() {
		return distanceCalculator;
	}

	public void setDistanceCalculator(DistanceCalc distanceCalculator) {
		this.distanceCalculator = distanceCalculator;
	}

	public ResponseData process (GeneralRequest genReq)
	{
/*		generalRequest = genReq;
	
		for(int i=0; i<generalRequest.getData().length; i++){
			//update((SendDataPar)generalRequest.getData()[i]);
		}
*/		
		return getResponseForStation((StationInfoDataKey)genReq.getKey());
	}
	
	private boolean update(SendDataPar sendDataPar){
		
		//provera stanice
		StationInfoValue stationInfoValue = (StationInfoValue)mainDataStructure.stationInfoData
			.getValue(new StationInfoDataKey(sendDataPar.getStationID()));
		
		if (stationInfoValue == null)
			return false;
		
		//provera linija koje sadrza ovu stanicu
		StationLineValue stationLineValue = (StationLineValue)mainDataStructure.stationLineData
			.getValue(new StationInfoDataKey(sendDataPar.getStationID()));
		

		//provera autobusa
		BusInfoValue busInfoValue = (BusInfoValue)mainDataStructure.busInfoData
			.getValue(new BusInfoDataKey(sendDataPar.getBusID()));
		if (busInfoValue==null)
			return false;
		
		//provera linije na kojoj je vozilo
		BusLineValue busLineValue = (BusLineValue)mainDataStructure.busLineData
			.getValue(new BusLineDataKey(sendDataPar.getBusID()));
		
		//trazenje zadnjeg pojavljivanja autobusa
		BusLastAppearanceValue busLastAppearanceValue = (BusLastAppearanceValue)mainDataStructure.busLastAppearanceData
			.getValue(new BusInfoDataKey(sendDataPar.getBusID()));
		
		///int newTime = distanceCalculator.calculate(busLastAppearanceValue, sendDataPar.getTime());
		
		//BusLastAppearanceValue newBusLastAppValue = new BusLastAppearanceValue(busLastAppearanceValue);
		//newBusLastAppValue.setTime(newTime);
		
		//TODO: odraditi message passing 
	
		
		return true;
	}
	
	private ResponseData getResponseForStation(StationInfoDataKey stationInfoDataKey){
		
		StationInfoValue stationInfoValue = (StationInfoValue)mainDataStructure.stationInfoData.getValue(stationInfoDataKey);
		
		
		ResponseData retVal = new ResponseData();
		/*
		retVal.getResponse().put(GeneralSettings.RESP_SEND_DATA);
		retVal.getResponse().putShort(stationInfoValue.getStationID().shortValue());
		*/
		return retVal;
	}
}
